package cn.iocoder.yudao.module.call.controller.admin.ivrkeywords;

import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivrkeywords.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrkeywords.IvrKeywordsDO;
import cn.iocoder.yudao.module.call.service.ivrkeywords.IvrKeywordsService;

@Tag(name = "管理后台 - ivr关键字")
@RestController
@RequestMapping("/call/ivr-keywords")
@Validated
public class IvrKeywordsController {

    @Resource
    private IvrKeywordsService ivrKeywordsService;

    @PostMapping("/create")
    @Operation(summary = "创建ivr关键字")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords:create')")
    public CommonResult<Long> createIvrKeywords(@Valid @RequestBody IvrKeywordsSaveReqVO createReqVO) {
        createReqVO.setType("2"); //固定为自定义类型
        return success(ivrKeywordsService.createIvrKeywords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr关键字")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords:update')")
    public CommonResult<Boolean> updateIvrKeywords(@Valid @RequestBody IvrKeywordsSaveReqVO updateReqVO) {
        ivrKeywordsService.updateIvrKeywords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr关键字")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords:delete')")
    public CommonResult<Boolean> deleteIvrKeywords(@RequestParam("id") Long id) {
        ivrKeywordsService.deleteIvrKeywords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr关键字")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords:query')")
    public CommonResult<IvrKeywordsRespVO> getIvrKeywords(@RequestParam("id") Long id) {
        IvrKeywordsDO ivrKeywords = ivrKeywordsService.getIvrKeywords(id);
        return success(BeanUtils.toBean(ivrKeywords, IvrKeywordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr关键字分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords:query')")
    public CommonResult<PageResult<IvrKeywordsRespVO>> getIvrKeywordsPage(@Valid IvrKeywordsPageReqVO pageReqVO) {
        PageResult<IvrKeywordsDO> pageResult = ivrKeywordsService.getIvrKeywordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrKeywordsRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得ivr关键字列表")
    public CommonResult<List<IvrKeywordsRespVO>> getIvrKeywordsList() {
        return success(BeanUtils.toBean(ivrKeywordsService.getIvrKeywordsSimpleList(), IvrKeywordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr关键字 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrKeywordsExcel(@Valid IvrKeywordsPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrKeywordsDO> list = ivrKeywordsService.getIvrKeywordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr关键字.xls", "数据", IvrKeywordsRespVO.class,
                BeanUtils.toBean(list, IvrKeywordsRespVO.class));
    }

    @PostMapping("/default/create")
    @Operation(summary = "创建ivr关键字-系统默认")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords-default:create')")
    public CommonResult<Long> createIvrKeywordsDefault(@Valid @RequestBody IvrKeywordsSaveReqVO createReqVO) {
        createReqVO.setType("1");
        return success(TenantUtils.execute(0L, () -> ivrKeywordsService.createIvrKeywords(createReqVO)));
    }

    @PutMapping("/default/update")
    @Operation(summary = "更新ivr关键字-系统默认")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords-default:update')")
    public CommonResult<Boolean> updateIvrKeywordsDefault(@Valid @RequestBody IvrKeywordsSaveReqVO updateReqVO) {
        TenantUtils.execute(0L, () -> ivrKeywordsService.updateIvrKeywords(updateReqVO));
        return success(true);
    }

    @DeleteMapping("/default/delete")
    @Operation(summary = "删除ivr关键字-系统默认")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords-default:delete')")
    public CommonResult<Boolean> deleteIvrKeywordsDefault(@RequestParam("id") Long id) {
        TenantUtils.execute(0L, () -> ivrKeywordsService.deleteIvrKeywords(id));
        return success(true);
    }

    @GetMapping("/default/get")
    @Operation(summary = "获得ivr关键字-系统默认")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords-default:query')")
    public CommonResult<IvrKeywordsRespVO> getIvrKeywordsDefault(@RequestParam("id") Long id) {
        IvrKeywordsDO ivrKeywords = TenantUtils.execute(0L, () -> ivrKeywordsService.getIvrKeywords(id));
        return success(BeanUtils.toBean(ivrKeywords, IvrKeywordsRespVO.class));
    }

    @GetMapping("/default/page")
    @Operation(summary = "获得ivr关键字分页-系统默认")
    @PreAuthorize("@ss.hasPermission('call:ivr-keywords-default:query')")
    public CommonResult<PageResult<IvrKeywordsRespVO>> getIvrKeywordsPageDefault(@Valid IvrKeywordsPageReqVO pageReqVO) {
        PageResult<IvrKeywordsDO> pageResult = TenantUtils.execute(0L, () -> ivrKeywordsService.getIvrKeywordsPage(pageReqVO));
        return success(BeanUtils.toBean(pageResult, IvrKeywordsRespVO.class));
    }

}